package com.example.layui_project.service.Impl;

import com.example.layui_project.comm.service.Impl.BaseServiceImpl;
import com.example.layui_project.dao.SupplierDao;
import com.example.layui_project.dao.UserDao;
import com.example.layui_project.entity.Supplier;
import com.example.layui_project.entity.SysUser;
import com.example.layui_project.service.SupplierService;
import com.example.layui_project.service.UserService;
import com.example.layui_project.utils.BestUitls;
import com.example.layui_project.utils.EasyExcelUtil;
import com.example.layui_project.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class SupplierServiceImpl extends BaseServiceImpl<Supplier> implements SupplierService {
    @Autowired
    private SupplierDao supplierDao;

    @Override
    public boolean add(Supplier supplier) {
        int i = supplierDao.insertSelective(supplier);
        return i > 0 ? true : false;
    }

    @Override
    public boolean del(Integer id) {
        int i = supplierDao.deleteByPrimaryKey(id);
        return i > 0 ? true : false;
    }

    @Override
    public boolean update(Supplier supplier) {
        int i = supplierDao.updateByPrimaryKeySelective(supplier);
        return i > 0 ? true : false;
    }

    @Override
    public boolean batchDel(List<Long> ids) {
        try {
            int batchDel = supplierDao.batchDel(ids);
            return batchDel > 0 ? true : false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<Supplier> selectAll() {
        try {
            return supplierDao.selectAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean importData(FileInputStream fis) {
        try {
            // 第一步：获取所有的sheet
            Map<String, List<List<String>>> readExcelAllSheet = EasyExcelUtil.readExcelAllSheet(fis);
            Set<String> keySet = readExcelAllSheet.keySet();
            for (String key : keySet) {
                List<List<String>> list = readExcelAllSheet.get(key);
                return addData(list, key);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
    @Transactional
    public boolean addData(List<List<String>> readExcel, String key) {
        try {
                readExcel.remove(0);
                for (List<String> list : readExcel) {
                    if (!BestUitls.isNotBlank(list.get(0))) {
                        continue;
                    }
                    //获取表格内容
                    String name = list.get(0) != null ? list.get(0) : "";
                    String phone = list.get(1) != null ? list.get(1) : "";
                    String address = list.get(2) != null ? list.get(2) : "";
                    String email = list.get(3) != null ? list.get(3) : "";
                    String type = list.get(4) != null ? list.get(4) : "";
                    //上传入库
                    Supplier supplier = new Supplier();
                    supplier.setName(name);
                    supplier.setPhone(phone);
                    supplier.setAddress(address);
                    supplier.setEmail(email);
                    supplier.setType(type);
                    boolean add = add(supplier);
                }
                return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
